package cn.edu.scau.cmi.zhouxinyue.spring.Controller;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.zhouxinyue.hibernate.domain.dao.StudentDAO;
import cn.edu.scau.cmi.zhouxinyue.hibernate.domain.dao.TeacherDAO;
import cn.edu.scau.cmi.zhouxinyue.hibernate.domain.factory.ScauCmiHibernateSessionFactory;
import cn.edu.scau.cmi.zhouxinyue.spring.util.ApplicationContextUtil;
import cn.edu.scau.cmi.zhouxinyue.hibernate.domain.*;
import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.TextArea;

public class studentController {
	@FXML
	private TextField id;
	@FXML
	private TextField name;
	@FXML
	private TextField teacher;
	@FXML
	private Button addstud;
	@FXML
	private TextField xid;
	@FXML
	private Button chastud;
	@FXML
	private TextField xteacher;
	@FXML
	private TextField xname;
	@FXML
	private TextArea op;
	@FXML
	private Button dstar;
	@FXML
	private TextField did;

	public void addstudent() {
		Session session = ScauCmiHibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();

		String n = name.getText();
		Long i = Long.parseLong(id.getText());
		Long tii=null;
		if(!teacher.getText().equals(""))
			tii = Long.parseLong(teacher.getText());
		

		Student s = new Student();
		
		ApplicationContext ac=ApplicationContextUtil.getApplicationContext();
		TeacherDAO td=(TeacherDAO) ac.getBean("TeacherDAO");
		
		s.setId(i);
		s.setName(n);
		if(tii!=null)
			s.setTeacher(td.findById(tii));

		StudentDAO SS = new StudentDAO();
		SS.save(s);
		transaction.commit();
		session.close();

		op.setText(null);
		StudentDAO sdao = new StudentDAO();
		List Student = sdao.findAll();
		Iterator<?> Ss = Student.iterator();
		while (Ss.hasNext()) {
			Student sc = (Student) Ss.next();
			String iii = Long.toString(sc.getId());
			if (sc.getTeacher() != null)
				op.appendText(sc.getName() + ' ' + iii + ' ' + sc.getTeacher().getName() + '\n');
			else
				op.appendText(sc.getName() + ' ' + iii + ' ' + '\n');
		}

	}

	public void changestudent() {
		Student s = null;
		ApplicationContext ac=ApplicationContextUtil.getApplicationContext();
		StudentDAO sd=(StudentDAO) ac.getBean("StudentDAO");
		
		TeacherDAO td=(TeacherDAO) ac.getBean("TeacherDAO");

		Session session = ScauCmiHibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();

		String n = xname.getText();
		String ii = xid.getText();
		String tn = xteacher.getText();
		Long ti = Long.parseLong(tn);
		Long i = Long.parseLong(ii);

		s = sd.findById(i);
		s.setName(n);
		s.setTeacher(td.findById(ti));
		sd.attachDirty(s);

		transaction.commit();
		session.close();

		op.setText(null);
		StudentDAO sdao = new StudentDAO();
		List Student = sdao.findAll();
		Iterator<?> Ss = Student.iterator();
		while (Ss.hasNext()) {
			Student sc = (Student) Ss.next();
			String iii = Long.toString(sc.getId());
			if (sc.getTeacher() != null)
				op.appendText(sc.getName() + ' ' + iii + ' ' + sc.getTeacher().getName() + '\n');
			else
				op.appendText(sc.getName() + ' ' + iii + ' ' + '\n');
		}

	}

	public void delstudent() {
		Session session = ScauCmiHibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		String id = did.getText();
		Long ID = Long.parseLong(id);

		ApplicationContext ac=ApplicationContextUtil.getApplicationContext();
		StudentDAO sd=(StudentDAO) ac.getBean("StudentDAO");
		
		Student s = sd.findById(ID);
		sd.delete(s);

		transaction.commit();
		session.close();

		op.setText(null);
		StudentDAO sdao = new StudentDAO();
		List Student = sdao.findAll();
		Iterator<?> Ss = Student.iterator();
		while (Ss.hasNext()) {
			Student sc = (Student) Ss.next();
			String iii = Long.toString(sc.getId());
			if (sc.getTeacher() != null)
				op.appendText(sc.getName() + ' ' + iii + ' ' + sc.getTeacher().getName() + '\n');
			else
				op.appendText(sc.getName() + ' ' + iii + ' ' + '\n');
		}
	}

}
